﻿using System;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Threading;

namespace TaskFactoryBugSample
{
  public class BackendService
  {
    private ServiceHost _host;
    private ManualResetEvent _manualResetEvent;

    public void Start()
    {
      _manualResetEvent = new ManualResetEvent(false);
      var thread = new Thread(Run);
      thread.Start();
    }

    private void Run()
    {
      _host = new ServiceHost(typeof(Backend));
      _host.Open(TimeSpan.FromSeconds(15));

      _manualResetEvent.WaitOne();

      _host.Close(TimeSpan.FromSeconds(15));
    }

    public Uri ServiceUri
    {
      get { return _host.BaseAddresses.First(); }
    }

    public void Stop()
    {
      _manualResetEvent.Set();
    }
  }
}